package com.fs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.entity.Materialcategory;
import com.fs.entity.Messages;
import com.fs.mapper.MessagesMapper;
import com.fs.service.IMessagesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-12
 */
@Service
public class MessagesServiceImpl implements IMessagesService {
    @Autowired
    MessagesMapper messagesMapper;
    @Override
    public List<Messages> SelectAllByid(Integer id) {
        QueryWrapper<Messages> messagesQueryWrapper=new QueryWrapper<>();
        messagesQueryWrapper.eq("receiver",id);
        List<Messages> messages = messagesMapper.selectList(messagesQueryWrapper);
        return messages;
    }
    @Override
    public Messages SelectMessagesbymid(Integer mid)
    {
        Messages messages = messagesMapper.selectById(mid);
        return messages;
    }

    @Override
    public boolean Deletebymids(Integer[] mid) {
        int rs=messagesMapper.deleteBatchIds(Arrays.asList(mid));
        if(rs!=0)
        {
            return true;
        }
        return false;
    }

    @Override
    public boolean Changeread(Integer mid) {
        QueryWrapper<Messages> queryWrapper=new QueryWrapper();
        queryWrapper.eq("msid",mid);
        Messages messages=new Messages();
        messages.setViewed(1);
        int rs=messagesMapper.update(messages,queryWrapper);
        if(rs!=0)
        {
            return true;
        }
        return false;
    }

    @Override
    public boolean AddMessage(Messages messages) {
        if(messages.getDate()==null)
        {
            messages.setDate(LocalDateTime.now());
        }
        int rs = messagesMapper.insert(messages);
        if(rs!=0)
        {

            return true;
        }
        return false;
    }
}
